# 导入:
# import mysql.connector.errorcode
import sqlalchemy
from sqlalchemy import Column, String, Integer, DateTime, Boolean
from utils.sql.db import session, Base
import time


# 定义User对象:
class Config(Base):
    # 表的名字:
    __tablename__ = 'config'

    # 表的结构:
    id = Column(Integer(), primary_key=True, default=None)
    phone = Column(Boolean)
    name = Column(Boolean)
    location = Column(Boolean)
    create_time = Column(String(20), default=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))


def config():
    try:
        configs = session.query(Config).filter(Config.id == 1).one()
        return {
            "code": 200,
            "data": configs,
            "message": "查询成功"
        }
    except sqlalchemy.exc.IntegrityError as e:
        return {
            "code": 201,
            "data": e,
            "message": "查询失败"
        }
        session.rollback()
        raise
    finally:
        # 关闭session:
        session.close()


def updateConfig(data):
    try:
        configs = session.query(Config).filter(Config.id == 1).one()
        configs.phone = data.phone
        configs.name = data.name
        configs.location = data.location
        # 提交事务
        session.commit()
        return {
            "code": 200,
            "data": configs,
            "message": "提交成功"
        }
    except sqlalchemy.exc.IntegrityError as e:
        return {
            "code": 201,
            "data": e,
            "message": "提交失败"
        }
        session.rollback()
        raise
    finally:
        # 关闭session:
        session.close()
